package com.xbongbong.saas.service.impl;/*/*/

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.domain.entity.PaasFormEntity;
import com.xbongbong.paas.model.PaasFormModel;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.fundset.pojo.FundSetTransitionPojo;
import com.xbongbong.saas.domain.entity.WorkOrderExplainEntity;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.model.WorkOrderExplainModel;
import com.xbongbong.saas.script.help.InitExplainsHelper;
import com.xbongbong.saas.service.WorkOrderExplainService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 *
 * @author 魏荣杰
 * @date 2019/3/18 14:31
 * @since v1.0
 * @version v1.0
 */
@Service("workOrderExplainService")
public class WorkOrderExplainServiceImpl implements WorkOrderExplainService {
    private static final Logger LOG = LoggerFactory.getLogger(WorkOrderExplainServiceImpl.class);

    @Resource
    private WorkOrderExplainModel workOrderExplainModel;
    @Resource
    private PaasFormModel paasFormModel;

    @Override
    public void saveDefaultExplain(String corpid, String userId, Long appId, Long formId, Long menuId) throws XbbException {
        try {
            WorkOrderExplainEntity explainEntity = new WorkOrderExplainEntity();
            explainEntity.setCorpid(corpid);
            explainEntity.setAppId(appId);
            explainEntity.setMenuId(menuId);
            explainEntity.setFormId(formId);
            PaasFormEntity paasFormEntity = paasFormModel.getByBusinessType(XbbRefTypeEnum.PRODUCT.getCode(),corpid);
            if (Objects.isNull(paasFormEntity)) {
                paasFormEntity = paasFormModel.getByBusinessTypeNoEnable(XbbRefTypeEnum.PRODUCT.getCode(),corpid);
            }
            FundSetTransitionPojo fundSetTransitionPojo = new FundSetTransitionPojo(corpid);
            explainEntity.setExplains(JSON.toJSONString(InitExplainsHelper.initFieldAttrEntity(XbbRefTypeEnum.WORK_ORDER.getAlias(), paasFormEntity, fundSetTransitionPojo, 0)));
            explainEntity.setCreatorId(userId);
            explainEntity.setDel(0);
            workOrderExplainModel.insert(explainEntity);
        } catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOG.error("WorkOrderExplainServiceImpl.saveDefaultExplain 添加表单解释信息出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
    }
}
